package com.example.demo;

import com.example.demo.dao.DemoDao;
import com.example.demo.service.DemoService;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.function.Consumer;

/*
 * 类描述:
 *使用sql+java代码统计2022-12-01至2022-12-09每天各类别商品的数量，按照商品入库日期进行排序，并使用test_dict翻译商品类别名称，输出结果示例：
		[
			{
				2022-12-01:[
					{
						tjlb:'LB001',
						tjlbmc:'排球',
						tjnum:10
					},
					{
						tjlb:'LB002',
						tjlbmc:'篮球',
						tjnum:10
					},
					{
						tjlb:'LB003',
						tjlbmc:'足球',
						tjnum:10
					}
				]
			},
			{
				2022-12-02:[
					{
						tjlb:'LB001',
						tjlbmc:'排球',
						tjnum:10
					},
					{
						tjlb:'LB002',
						tjlbmc:'篮球',
						tjnum:10
					},
					{
						tjlb:'LB003',
						tjlbmc:'足球',
						tjnum:10
					}
				]
			},
			{
				2022-12-03:[
					{
						tjlb:'LB001',
						tjlbmc:'排球',
						tjnum:10
					},
					{
						tjlb:'LB002',
						tjlbmc:'篮球',
						tjnum:10
					},
					{
						tjlb:'LB003',
						tjlbmc:'足球',
						tjnum:10
					}
				]
			},...
		]
 *
 * @Author: Administrator
 * @Date: 2023/2/23
 * @Version: 1.0.0
 */
@SpringBootTest
public class Exam3Testor {


    @Autowired
    private DemoService demoService;
    @Autowired
    private DemoDao demoDao;


    @Test
    void m3() {
        Map<String, String> param = new HashMap();
        param.put("startDate", "2022-12-01");
        param.put("endDate", "2022-12-09");

        try {
            List<LinkedHashMap<String, String>> resultD = demoDao.examtest3(param);
            LinkedHashSet<String> datakeyset = new LinkedHashSet<>();

            //获取key
            resultD.forEach(new Consumer<LinkedHashMap<String, String>>() {
                @Override
                public void accept(LinkedHashMap<String, String> smap) {
                    datakeyset.add(String.valueOf(smap.get("tjrq")));
                }
            });


            //拼接数据
            LinkedHashMap<String, Object> slinkedhashmaps = new LinkedHashMap<String,Object>();
            for (String key : datakeyset) {
                LinkedList<Object> objects = new LinkedList<>();

                for (LinkedHashMap<String, String> smap : resultD) {
                    String tjrq = String.valueOf(smap.get("tjrq"));
                    String tjlb = StringUtils.isNotBlank(smap.get("tjlb")) ? smap.get("tjlb") : "";
                    String tjlbmc = StringUtils.isNotBlank(smap.get("tjlbmc")) ? smap.get("tjlbmc") : "";
                    String tjnum = StringUtils.isNotBlank(String.valueOf(smap.get("tjnum"))) ? String.valueOf(smap.get("tjnum")) : "";
                    if (key.equals(tjrq)) {
                        LinkedHashMap<String, Object> temp = new LinkedHashMap<>();
                        temp.put("tjlb", tjlb);
                        temp.put("tjlbmc", tjlbmc);
                        temp.put("tjnum", tjnum);
                        objects.add(temp);
                    }
                }
                slinkedhashmaps.put(key,objects);
            }

//            System.out.println(slinkedhashmaps);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
